package edu.hawaii.myisern.users;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class for managing users.
 * @author John Hauge
 *
 */
public class UserManager {
  private List<User> userList;
  private static Logger logger = Logger.getLogger("edu.hawaii.myisern.Hauge");
  
  /**
   * Constructor. 
   */
  public UserManager() {
    userList = new ArrayList<User>();
    try {
      userList.add(new User("isern", "isern2007"));
      userList.add(new User("root", "toor"));
    } catch (NoSuchAlgorithmException e) {
      logger.log(Level.SEVERE, e.toString());
    }
  }
  
  /**
   * Authenticates a user name and password.
   * @param user Username.
   * @param password Password.
   * @return True/False.
   */
  public boolean authenticate(String user, String password) {
    try {
      boolean pass = false;
      User u = new User(user, password);
      if (userList.contains(u)) {
        pass = true;
      }
      return pass;
    }
    catch (Exception e) {
      logger.log(Level.SEVERE, e.toString());
      return false;
    }
  }
  /**
   * Method for adding new users to the list.
   * @param user Username.
   */
  public void addUser(User user) {
    try {
      if (!userList.contains(user)) {
        userList.add(user);
      }
    }
    catch (Exception e) {
      logger.log(Level.SEVERE, e.toString());
    }
  }
  
}
